package com.bdqn.service;

import com.bdqn.dao.UserDao;
import com.bdqn.dao.UserDaoImpl;
import com.bdqn.entity.User;
import com.bdqn.factory.UserDaoFactory;
import org.apache.log4j.Logger;

/**
 * 用户模块业务层实现
 */
public class UserServiceImpl implements UserService {
    private static final Logger log = Logger.getLogger(UserServiceImpl.class);
    //    //实例化所依赖的UserDao对象
//    private UserDao userDao = new UserDaoImpl();
//    private UserDao userDao = UserDaoFactory.getInstance();
    /**
     * 声明接口类型的引用，和具体实现类解耦合
     */
    private UserDao userDao;
    /**
     * 保存用户信息
     *
     * @param user
     */
    @Override
    public void save(User user) {
        //调用UserDao的方法保存用户信息
        userDao.save(user);
    }

    /**
     * dao属性的setter访问器，会被Spring调用，实现依赖注入
     * @param userDao
     */
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    //    public boolean save1(User user) {
//        log.info("添加用户 " + user.getUsername());
//        SqlSession sqlSession = null;
//        boolean flag = false;
//        try {
//            sqlSession = MyBatisUtil.createSqlsession();
//            if (sqlSession.getMapper(UserMapper.class).add(user) > 0) {
//                flag = true;
//                sqlSession.commit();
//                log.info("成功添加用户" + user.getUsername());
//            }
//        } catch (Exception e) {
//            log.info("添加用户" + user.getUsername() + "失败", e);
//            sqlSession.rollback();
//            flag = false;
//        } finally {
//            MyBatisUtil.closeSqlsession(sqlSession);
//        }
//        return false;
//    }
}
